package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.JyjcImportSebs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.JyjcImportYwbtjsrzcMapper;
import com.ruoyi.system.domain.JyjcImportYwbtjsrzc;
import com.ruoyi.system.service.IJyjcImportYwbtjsrzcService;

/**
 * 业务部统计的各专业所业务收入和支出情况Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-19
 */
@Service
public class JyjcImportYwbtjsrzcServiceImpl implements IJyjcImportYwbtjsrzcService 
{
    private static final Logger log = LoggerFactory.getLogger(JyjcImportYwbtjsrzcServiceImpl.class);
    @Autowired
    private JyjcImportYwbtjsrzcMapper jyjcImportYwbtjsrzcMapper;

    /**
     * 查询业务部统计的各专业所业务收入和支出情况
     * 
     * @param id 业务部统计的各专业所业务收入和支出情况主键
     * @return 业务部统计的各专业所业务收入和支出情况
     */
    @Override
    public JyjcImportYwbtjsrzc selectJyjcImportYwbtjsrzcById(Long id)
    {
        return jyjcImportYwbtjsrzcMapper.selectJyjcImportYwbtjsrzcById(id);
    }

    /**
     * 查询业务部统计的各专业所业务收入和支出情况列表
     * 
     * @param jyjcImportYwbtjsrzc 业务部统计的各专业所业务收入和支出情况
     * @return 业务部统计的各专业所业务收入和支出情况
     */
    @Override
    public List<JyjcImportYwbtjsrzc> selectJyjcImportYwbtjsrzcList(JyjcImportYwbtjsrzc jyjcImportYwbtjsrzc)
    {
        return jyjcImportYwbtjsrzcMapper.selectJyjcImportYwbtjsrzcList(jyjcImportYwbtjsrzc);
    }

    /**
     * 新增业务部统计的各专业所业务收入和支出情况
     * 
     * @param jyjcImportYwbtjsrzc 业务部统计的各专业所业务收入和支出情况
     * @return 结果
     */
    @Override
    public int insertJyjcImportYwbtjsrzc(JyjcImportYwbtjsrzc jyjcImportYwbtjsrzc)
    {
        return jyjcImportYwbtjsrzcMapper.insertJyjcImportYwbtjsrzc(jyjcImportYwbtjsrzc);
    }

    /**
     * 修改业务部统计的各专业所业务收入和支出情况
     * 
     * @param jyjcImportYwbtjsrzc 业务部统计的各专业所业务收入和支出情况
     * @return 结果
     */
    @Override
    public int updateJyjcImportYwbtjsrzc(JyjcImportYwbtjsrzc jyjcImportYwbtjsrzc)
    {
        return jyjcImportYwbtjsrzcMapper.updateJyjcImportYwbtjsrzc(jyjcImportYwbtjsrzc);
    }

    /**
     * 批量删除业务部统计的各专业所业务收入和支出情况
     * 
     * @param ids 需要删除的业务部统计的各专业所业务收入和支出情况主键
     * @return 结果
     */
    @Override
    public int deleteJyjcImportYwbtjsrzcByIds(Long[] ids)
    {
        return jyjcImportYwbtjsrzcMapper.deleteJyjcImportYwbtjsrzcByIds(ids);
    }

    /**
     * 删除业务部统计的各专业所业务收入和支出情况信息
     * 
     * @param id 业务部统计的各专业所业务收入和支出情况主键
     * @return 结果
     */
    @Override
    public int deleteJyjcImportYwbtjsrzcById(Long id)
    {
        return jyjcImportYwbtjsrzcMapper.deleteJyjcImportYwbtjsrzcById(id);
    }

    @Override
    public String importJyjcImportYwbtjsrzc(List<JyjcImportYwbtjsrzc> jyjcImportYwbtjsrzcList, String yearyf) {
        String [] sj=yearyf.split("-");

        if (StringUtils.isNull(jyjcImportYwbtjsrzcList) || jyjcImportYwbtjsrzcList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        jyjcImportYwbtjsrzcMapper.deleteJyjcImportYwbtjsrzcByYearYf(sj[0]+"-"+sj[1]);
        for (JyjcImportYwbtjsrzc jyjcImportYwbtjsrzc: jyjcImportYwbtjsrzcList)
        {
            try
            {
                jyjcImportYwbtjsrzc.setYear(sj[0]);
                jyjcImportYwbtjsrzc.setYf(sj[1]);
                jyjcImportYwbtjsrzc.setYearyf(sj[0]+"-"+sj[1]);
                jyjcImportYwbtjsrzcMapper.insertJyjcImportYwbtjsrzc(jyjcImportYwbtjsrzc);
                successNum++;
                successMsg.append("<br/>" + successNum + "、 " + sj[0]+"年"+sj[1]+"月"+jyjcImportYwbtjsrzc.getDeptname()  + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + sj[0]+"年"+sj[1]+"月"+jyjcImportYwbtjsrzc.getDeptname()   + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
